<?php

/*-----------------------------------------------------------*\
|                 - Модуль публікацій -                      |
|                 Author:  Skalden Russell                   |
|                 Last modify: 20130105                      |
\*-----------------------------------------------------------*/

// Захист скрипта від довільного запуску
if(!defined('TSUE_SEC')) {
	header('Content-Type: text/html; charset=utf-8');
	@require_once( '../../data/messages/hacking.attempt.php' );
	die( $TSUE_MSG );
}
	
define( "PUBLIC_PER_PAGE", $MALINA_CONF['public_pp'] );

	// Кодування / декодування тексту
	$sectext = new CSecurity();
	
	// База Даних
	$sql = new cMysql();
	$sql->connect($SQL_CONF['host'], $SQL_CONF['user'], $SQL_CONF['pass'], $SQL_CONF['dbname']);
	
	///////////////////////////////////////////////////////
	// Відобразити повний матеріал
	if($_GET['act'] == "viewfull")
	{
		$_GET['id'] = $sectext->digit( $_GET['id'] );
		$_GET['id'] = $sql->sec_check( $_GET['id'] );
		
		// запит матеріалу
		$mat_rows = $sql->fetch_array("SELECT * FROM `" . $SQL_CONF['pref'] ."materialpublic` WHERE `materialid`='" . $_GET['id'] . "'");
		$sql->query("UPDATE `" . $SQL_CONF['pref'] . "materialpublic` SET `views` = '" . $sectext->digit($mat_rows[0]['views'] + 1) . "' WHERE `materialid` ='" . $_GET['id'] . "'");
		
		// Визначення місця прибуття
		$_GET['catid'] = $sectext->digit( $_GET['catid'] );
		$from = '';
		if( $_GET['catid'] == 0 ) {
			$from = "materials";
		} else {
			$from = "public";
		}
		
		$pContent->SetReplace( "from",		$from );
		$pContent->SetReplace( "title",		$sectext->decode( $mat_rows[0]['title'] ) );
		$pContent->SetReplace( "pagefull",	$sectext->decode( $mat_rows[0]['pagefull'] ) );
		$pContent->SetReplace( "author",	$sectext->decode( $mat_rows[0]['author'] ) );
		$pContent->SetReplace( "views",		$sectext->digit( $mat_rows[0]['views'] + 1 ) );
		$pContent->SetReplace( "date",		$sectext->decode( $mat_rows[0]['date'] ) );
		$pContent->SetReplace( "catid",		$_GET['catid'] );
		
		$TSUE_CONTENT .= $pContent->getContent( 'styles/' . $MALINA_CONF['theme_cur'] . '/table_materialfull.html', 'r' );
		
	}
	else
	{
		// Перевірка catid
		$_GET['catid'] = $sectext->digit( $_GET['catid'] );
		$_GET['catid'] = $sql->sec_check( $_GET['catid'] );
		
		$mat_count = $sql->fetch_assoc("SELECT COUNT(*) as count FROM `". $SQL_CONF['pref'] ."materialpublic` WHERE `categoryid`='". $_GET['catid'] ."'");
		
		$ppage = $sql->sec_check( $sectext->digit( $_GET['ppage'] ) );
		$mat_splitter = make_splitter( $ppage, $mat_count[0]['count'], PUBLIC_PER_PAGE, '?p=public&catid='. $_GET['catid'] .'&ppage=' );
		
		// запит матеріалу
		$mat_rows = $sql->fetch_array("SELECT `materialid`, `title`, `pagedescr`, `author`, `views`, `date` FROM `" . $SQL_CONF['pref'] ."materialpublic` WHERE `categoryid`='" . $_GET['catid'] . "' LIMIT " . $mat_splitter['begin'] . ", " . PUBLIC_PER_PAGE . " ");
		
		
		// Контент статей
		$TSUE_CONTENT = "<br /><h1>Статьи</h1><br />";
		$mat_size = count( $mat_rows ) - 1;
		if( $mat_size == 0 )
		{
			$TSUE_CONTENT .= "Матеріали відсутні";
		} 
		else 
		{
			for($i = 0; $i < $mat_size; $i++)
			{
				$pContent->SetReplace( "title",		$sectext->decode( $mat_rows[$i]['title'] ) );
				$pContent->SetReplace( "description",	$sectext->decode( $mat_rows[$i]['pagedescr'] ) );
				$pContent->SetReplace( "author",	$sectext->decode( $mat_rows[$i]['author'] ) );
				$pContent->SetReplace( "views",		$sectext->digit( $mat_rows[$i]['views'] ) );
				$pContent->SetReplace( "materialid",	$sectext->digit( $mat_rows[$i]['materialid'] ) );
				$pContent->SetReplace( "catid",		$_GET['catid'] );
				$pContent->SetReplace( "date",		$sectext->decode( $mat_rows[$i]['date'] ) );
				
				$TSUE_CONTENT .= $pContent->getContent( 'styles/' . $MALINA_CONF['theme_cur'] . '/table_material.html', 'r' );
			}
		}

		$TSUE_CONTENT .= $mat_splitter['splitter'];
	}
	
	$sql->close();
?>